<template>
  <div>
    <el-card class="box-card">
      <div class="news_header">
        <MainSearchForm ref="merchantIndexRef" :formList="formList" :formValue="formValue" @formReset="formChange" @formSearch="formChange"></MainSearchForm>
        <!-- <el-button size="mini" type="primary" class="add-btn" @click="addNews">新增</el-button> -->
      </div>
      <div style="min-height: 600px">
        <MainTable
          ref="orderTable"
          :loading="listLoading"
          :columns="columns"
          :tableData="tableData"
          showSelection
          @selection-change="handleSelectionChange"
        >
          <template v-slot:status="{ row }">
            <span :style="{ color: getStatusText(row.status, 1) }">{{ getStatusText(row.status, 2) }}</span>
          </template>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <MainLink title="审核" color="#F56C6C" @linkClick="examine(scope.row)"></MainLink>
            </template>
          </el-table-column>
        </MainTable>
      </div>

      <div class="pagination-right">
        <MainPagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout=" ->,total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </MainPagination>
      </div>
      <MainDialog
        title="审核商户"
        :showDialog="examineVisible"
        width="480px"
        @close="handleClose">
        <template v-slot:content>
          <el-form :model="examineForm" :rules="rules" label-position="right" label-width="100px" ref="examineFormRef">
            <el-form-item label="商户名称：" prop="company_name">
              <el-input v-model="examineForm.company_name" disabled></el-input>
            </el-form-item>
            <el-form-item label="选择状态：" prop="status">
              <div class="flex-center">
                <div v-for="item in typeList" :key="item.label" :class="{ active: examineForm.status === item.value }" @click="changeType(item)">
                  {{ item.label }}
                </div>
              </div>
            </el-form-item>
            <div class="flex">
              <div>
                <span>审核人：</span>
                <span>{{ examineForm.name }}</span>
              </div>
              <div>
                <span>审核日期：</span>
                <span>{{ examineForm.time }}</span>
              </div>
            </div>
          </el-form>
        </template>
        <template v-slot:footer>
          <div class="main-dialog-btn align-right">
            <el-button type="success" class="add-btn" @click="handleClose">取消</el-button>
            <el-button type="primary" class="submit" @click="submitExamine">确定</el-button>
          </div>
        </template>
      </MainDialog>
    </el-card>
  </div>
</template>

<script>
import { getMerchantList, getMerchantTotal, auditMerchant } from "@/api/merchant";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { mapGetters } from "vuex";
import dayjs from "dayjs";
import MainTable from '@/components/MainTable/table.vue'
import { indexData } from '../MockData'
const { columns, formList, formValue, tabsList } = indexData
export default {
  name: "news",
  components: {
    MainTable
  },
  computed: {
    ...mapGetters(["userInfo"]),
  },
  data() {
    return {
      handleScrool: "",
      formList,
      formValue,
      sendTime: [],
      tableData: [],
      currentPage: 1,
      pageSize: 20,
      total: 0,
      // 勾选的数据
      multipleSelection: [],
      // 新闻日期
      pickerOptions: [],
      listLoading: false,
      dialogData:{
        title:"",
        status:false,
        mydata:{}
      },
      columns,
      tabsList,
      examineVisible: false,
      examineForm: {
        name: '',
        time: '',
        company_name: '',
        status: '1'
      },
      rules: {
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' },
        ]
      },
      typeList: [
        { label: '正常', value: '1' },
        { label: '禁用', value: '0' },
        { label: '审核', value: '2' },
      ],
      rowData: {}
    };
  },
  activated() {
    this.getData();
    this.getTabsNum()
  },

  methods: {
    handleClose() {
      this.examineVisible = false
      this.examineForm.audit_remark = ''
      this.rowData = {}
    },
    examine(row) {
      this.examineForm.company_name = row.nickname
      this.examineForm.name = this.userInfo.name
      this.examineForm.time = dayjs(new Date()).format("YYYY-MM-DD")
      this.rowData = row
      this.examineVisible = true
    },
    changeType(item) {
      this.examineForm.status = item.value
    },
    submitExamine() {
      auditMerchant({
        id: this.rowData.id,
        company_id: this.rowData.company_id,
        status: this.examineForm.status
      }).then(res => {
        if(res.errcode == 0) {
          this.$message.success('审核完成')
          setTimeout(() => {
            this.handleClose()
            this.handleClick()
          }, 1500)
        }else {
          this.$message.error(res.errmsg)
        }
        
      })
    },
    //  表格-状态转化
    getStatusText(status, type) {
      const statusObj = {
        0: {
          text: '禁用',
          color: '#999999'
        },
        1: {
          text: '正常',
          color: '#03A98E'
        },
        2: {
          text: '待审核',
          color: '#E27C21'
        },
      }
      return type === 1 ? statusObj[status].color : statusObj[status].text
    },
    handleClick() {
      this.$refs.merchantIndexRef.getFormRefs().resetFields();
      this.formChange()
    },
    //  表单搜索
    formChange() {
      this.currentPage = 1
      this.pageSize = 20
      this.getData()
    },
    getTabsNum() {
      getMerchantTotal({}).then(res => {
        if(res.errcode == 0) {
          this.tabsList.forEach(el => {
            el.num = res.data[el.key]
          })
        }
      })
    },
    getData() {
      this.listLoading = true;
      let data = {
        page: this.currentPage,
        page_size: this.pageSize,
        // ...this.formValue
        status: 2
      };
      getMerchantList(data).then(
        (res) => {
          this.listLoading = false;
          this.tableData = res.data.list;
          this.total = res.data.total;
        },
        (err) => {
          console.log(err);
        }
      );
    },
    editBanner(row, pageControls) {
      // 修改新闻
      this.$router.push({
        path: "/news/banneradd",
        query: {
          id: row.id,
          pageControls
        }
      });
    },
    close(){
      this.dialogData.status=false;
      location.reload()
    },
    deleteNews(row) {
      // 删除新闻
      let query = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: row.id }
      );
      deleteBannerList(query).then(
        (res) => {
          if (res.errcode == 0) {
            this.$message.success("删除成功");
            this.getData();
          } else {
            this.$message.error(res.errmsg);
          }
        },
        (err) => {
          console.log(err);
        }
      );
    },
    // 状态
    changeSwitch(e, row) {
      console.log("1", e, row);
    },
    // 勾选的数据
    handleSelectionChange(val) {
      console.log("val", val);
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },
  },
  created() {
  },
};
</script>
<style lang="scss" scoped>
@import '../index.scss';
.box-card {
  .pagination-right {
    @include flex(flex-end, center);
  }
  /deep/.el-card__body {
    padding: 20px;
  }
}
</style>
